function getBaseUrl()
{
	var l = window.location;
	var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];
	return base_url;
}
function handleFileUpload(files)
{
   for (var i = 0; i < files.length; i++)
   {
        var fd = new FormData();
        fd.append('file', files[i]);
        sendFileToServer(fd);
 
   }
}
function sendFileToServer(formData)
{

    var uploadURL = getBaseUrl()+'/receipts/recieptupload'; //Upload URL
    $('#spinter-uploading').show();
    $.ajax({
        url: uploadURL,
        type: "POST",
        contentType:false,
        processData: false,
        cache: false,
        data: formData,
        success: function(data){
        $('#allReceiptsContainer').prepend(data);    
    	$('#spinter-uploading').hide();
        }
    });
 
}
$(document).ready(function(){
	var obj = $("#qq-upload-drop-area");
	obj.on('dragenter', function (e)
	{
	    e.stopPropagation();
	    e.preventDefault();
	    $(this).css('border', '2px solid #0B85A1');
	});
	obj.on('dragover', function (e)
	{
	     e.stopPropagation();
	     e.preventDefault();
	});
	obj.on('drop', function (e)
	{
	     $(this).css('border', '2px dotted #0B85A1');
	     e.preventDefault();
	     var files = e.originalEvent.dataTransfer.files;
	 
	     //We need to send dropped files to Server
	     handleFileUpload(files);
	    
	});
	
	$(document).on('dragenter', function (e)
	{
	    e.stopPropagation();
	    e.preventDefault();
	});
	$(document).on('dragover', function (e)
	{
	  e.stopPropagation();
	  e.preventDefault();
	  obj.css('border', '2px dotted #0B85A1');
	});
	$(document).on('drop', function (e)
	{
	    e.stopPropagation();
	    e.preventDefault();
	});

	$('#receipts_selectAllCheckbox').click(function(){
		$('.receipts_selector_checkbox:input:checkbox').not(this).prop('checked', this.checked);
	});
			
	$('#receipts_batchDelete').click(function(){
		    var uploadURL = getBaseUrl()+'/receipts/delete'; //Upload URL
		    //$('#spinter-delete').show();
		    var deleteString = '';
		    $('input:checkbox.receipts_selector_checkbox').each(function () {
		        var sThisVal = (this.checked ? $(this).val() : "");
		        deleteString+=sThisVal+'|';
		   });
		    $.ajax({
		        url: uploadURL,
		        type: "POST",
		        data: 'deleteString='+deleteString,
		        success: function(data){
		        }
		    });
		    $('input:checkbox.receipts_selector_checkbox').each(function () {
		    	$(this).parent().parent().parent().remove();
		   });
	});
	
	$(document).on("click", ".delete_receipt_button", function() {
	    var uploadURL = getBaseUrl()+'/receipts/deleteeach';
	    var idString = $(this).attr('id');
	    var idSplit = idString.split('_');
	    var deleteId = idSplit[1];
	    $.ajax({
	        url: uploadURL,
	        type: "POST",
	        data: 'delete_id='+deleteId
	    });	
	    $(this).parent().parent().parent().parent().remove();
	});
	$('#btn_search').click(function(){
	    var uploadURL = getBaseUrl()+'/receipts/search'; //Upload URL
	    //$('#spinter-delete').show();
	    var values = $('#filter').serialize();
	    $.ajax({
	        url: uploadURL,
	        type: "POST",
	        data: values,
	        success: function(data){
	    	    $('#allReceiptsContainer').html(data);
	        }
	    });
	});	
	$('#button_importButton').click(function(){
		$('#file').click();
	});
	$('#file').change(function(){
		 var filedata = document.getElementById("file");
		 handleFileUpload(filedata.files);
	});
	$('#btn_reset').click(function(){
		$("#filter")[0].reset();
	});
	$('#receipts_addReport').click(function(){
		
		 var reportId = $('#receipts_reportSelector').val();
		 if(reportId == 0)
			 return false;
		$('#addreport_spinner').show();
	    var uploadURL = getBaseUrl()+'/receipts/addreceiptstoreport'; //Upload URL
	    //$('#spinter-delete').show();
	    var receiptString = '';
	    $('input:checkbox.receipts_selector_checkbox').each(function () {
	        var sThisVal = (this.checked ? $(this).val() : "");
	        receiptString+=sThisVal+'|';
	   });
	   
	    $.ajax({
	        url: uploadURL,
	        type: "POST",
	        data: 'receiptString='+receiptString+'&reportId='+reportId,
	        success: function(data){
	    		$('#addreport_spinner').hide();
	        }
	    });
	    
	});
	
	
});
$(function() {
	$( ".semwal_dtpicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
	});
